package com.newcoin.chat.util;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.nio.file.FileSystems;
import java.nio.file.Path;

public class QRCodeUtil {
//    private static final String QR_CODE_IMAGE_PATH = "D:\\static\\qrcode\\"; //dev路径
    private static final String QR_CODE_IMAGE_PATH = "/root/chatFile/qrcode/";//生产及测试

    public static void generateQRCodeImage(String text, int width, int height, String QRpath) {
        try {
            QRCodeWriter qrCodeWriter = new QRCodeWriter();
            BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height);
//            Resource resource = new ClassPathResource("");
//            //resource.getFile().getAbsolutePath()+
            Path path = FileSystems.getDefault().getPath(QRpath + text + ".png");
            MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
